package dyq.example.com;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.pm.PackageManager;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Fragment mTab01 = new weixinFragment();
    private Fragment mTab02 = new frdFragment();
    private Fragment mTab03 = new contactFragment();
    private Fragment mTab04 = new settingsFragment();

    private FragmentManager fm;

    private LinearLayout mTabweixin;
    private LinearLayout mTabfrd;
    private LinearLayout mTabcontact;
    private LinearLayout mTabsettings;

    private ImageButton mImgweixin;
    private ImageButton mImgfrd;
    private ImageButton mImgcontact;
    private ImageButton mImgsettings;


    private List<String> mList = new ArrayList<>();

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case 1:
                if(grantResults[0]!= PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(this, "未授权，无法实现预定的功能！", Toast.LENGTH_SHORT).show();
                    finish();
                }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        去掉title
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();
        initFragment();
        initEvent();
        setSelect(0);

    }

    //    initialize view
    private void initView(){
        mTabweixin = (LinearLayout)findViewById(R.id.id_tab_weixin);
        mTabfrd = (LinearLayout)findViewById(R.id.id_tab_frd);
        mTabcontact = (LinearLayout)findViewById(R.id.id_tab_contact);
        mTabsettings = (LinearLayout)findViewById(R.id.id_tab_settings);

        mImgweixin = (ImageButton)findViewById(R.id.id_tab_weixin_img);
        mImgfrd = (ImageButton)findViewById(R.id.id_tab_frd_img);
        mImgcontact = (ImageButton)findViewById(R.id.id_tab_contact_img);
        mImgsettings = (ImageButton)findViewById(R.id.id_tab_settings_img);
    }


    //    initialize fragment
    private void initFragment(){
        //获得当前环境下的fm
        fm = getSupportFragmentManager();
        //开始进行通信
        FragmentTransaction transaction = fm.beginTransaction();
        //把mtab压入framlayout
        transaction.add(R.id.id_content,mTab01);
        transaction.add(R.id.id_content,mTab02);
        transaction.add(R.id.id_content,mTab03);
        transaction.add(R.id.id_content,mTab04);
        transaction.commit();
    }


    //监听用户点击到哪一个LinearLayout区域，将i设置为对应数值（0/1/2/3）
    //将监听过程限制在这4个LinearLayout上，这四个函数监听函数统一用onClick()响应
    private void initEvent(){
        mTabweixin.setOnClickListener(this);
        mTabfrd.setOnClickListener(this);
        mTabcontact.setOnClickListener(this);
        mTabsettings.setOnClickListener(this);
    }


    //写一个函数来控制图片颜色变换，i是由点击造成的
    //每次仅有一个绿的，其他都是灰的
    private void setSelect(int i){
        //一个新的Fragment控制
        String s = String.valueOf(i);
        Log.d("select","s");
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        //把图片设置为亮绿色（0/1/2/3）
        //设置区域内容
        switch (i){
            case 0:
                Log.d("setSelect","1");
                transaction.show(mTab01);
                //此时要把图片标绿
                mImgweixin.setImageResource(R.drawable.tab_weixin_pressed);
                break;
            case 1:
                transaction.show(mTab02);
                mImgfrd.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case 2:
                transaction.show(mTab03);
                mImgcontact.setImageResource(R.drawable.tab_address_pressed);
                break;
            case 3:
                transaction.show(mTab04);
                mImgsettings.setImageResource(R.drawable.tab_settings_pressed);
                break;
            default:
                break;
        }
        transaction.commit();//整个结束
    }


    //把四个tab先全部隐藏起来，等后面再show()
    private void hideFragment(FragmentTransaction transaction){
        Log.d("hide","1");
        transaction.hide(mTab01);
        transaction.hide(mTab02);
        transaction.hide(mTab03);
        transaction.hide(mTab04);
    }

//    send the number corresponding the clicked area to function setSelect
    @Override
    public void onClick(View v) {//v视图对象
        Log.d("onClick","1");
        resetImgs();
        switch (v.getId()){
            case R.id.id_tab_weixin:
                Log.d("onClick","2");
                setSelect(0);
                break;
            case R.id.id_tab_frd:
                setSelect(1);
                break;
            case R.id.id_tab_contact:
                setSelect(2);
                break;
            case R.id.id_tab_settings:
                setSelect(3);
                break;
            default:
                break;

        }
    }

    //让所有图片变灰
    private void resetImgs() {
        mImgweixin.setImageResource(R.drawable.tab_weixin_normal);
        mImgfrd.setImageResource(R.drawable.tab_find_frd_normal);
        mImgcontact.setImageResource(R.drawable.tab_address_normal);
        mImgsettings.setImageResource(R.drawable.tab_settings_normal);
    }

}
